<?php
/**
 * This file is part of Hyperf Mall.
 *
 * @link     https://gitee.com/scwlkj/hyperf_mall
 * @contact  hyperf_mall@163.com
 */

namespace App\JsonRpc;

use App\Exception\BaseException;
use App\JsonRpc\Interfaces\BaseSpecServiceInterface;
use App\Repository\BaseSpecRepository;
use Hyperf\RpcServer\Annotation\RpcService;
use Hyperf\Di\Annotation\Inject;

/**
 * @RpcService(name="Goods::BaseSpecService", protocol="jsonrpc", server="jsonrpc")
 */
class BaseSpecService extends BaseService implements BaseSpecServiceInterface
{

    public function init()
    {
        $this->repository = BaseSpecRepository::getInstance();
    }

    /**
     * Notes: 添加标签
     * Author: Bruce.z
     * DateTime: 2021/12/22 20:17
     * @param array $data
     * @return \App\Model\BaseModel|\Hyperf\Database\Model\Model
     * @throws BaseException
     */
    public function add(array $data)
    {
        $one = $this->repository->getOne(['title'=>$data['title']]);
        if(count($one)){
            throw new BaseException(BaseException::BASE_ERROR, '名称已存在');
        }
        return $this->repository->createSpec($data);
    }

    /**
     * Notes: 详情
     * Author: Bruce.z
     * DateTime: 2022/1/13 14:16
     * @param int $id
     * @return array
     */
    public function detail(int $id): array
    {
        return $this->repository->detailSpec($id);
    }

    /**
     * Notes: 编辑
     * Author: Bruce.z
     * DateTime: 2022/1/13 14:15
     * @param int $id
     * @param array $data
     * @return bool
     */
    public function update(int $id, array $data): bool
    {
        return $this->repository->updateSpec($id, $data);
    }

    public function delete(int $id): bool
    {
        return $this->repository->deleteSpec($id);
    }
}
